﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using Pavilion.DesignerTool.ViewTools;
using Pavilion.DesignerTool.ViewTools.ConnectableListBox;

namespace Pavilion.DesignerTool
{
    /// <summary>
    /// Interaction logic for NodeDesignerToolView.xaml
    /// </summary>
    public partial class NodeDesignerToolView : UserControl
    {
        private NodeDesignerToolViewModel viewModel;
        public NodeDesignerToolViewModel ViewModel
        {
            get { return viewModel; }
            private set
            {
                viewModel = value;
                DataContext = viewModel;
            }
        }

        public NodeDesignerToolView()
        {
            this.ViewModel = new NodeDesignerToolViewModel();
            this.ViewModel.SelectedItemChanged += new Action<object>(ViewModel_SelectedItemChanged);

            InitializeComponent();
        }

        public void LoadResourceItem(ResourceData animationResourceData)
        {
            LoadNodeScript(animationResourceData.NodeScriptWrapperCollection.First());
        }

        public void LoadNodeScript(NodeScriptWrapper nodeScriptWrapper)
        {
            gameDataEditorView.SetGameData(null);
            lockEditorView.SetLink(null);

            viewModel.LoadNodeScript(nodeScriptWrapper);
        }

        private void LoadGameDataEditor(NodeBaseWrapper nodeWrapper)
        {
            gameDataEditorView.SetGameData(nodeWrapper);

            if (propertiesPanel.Visibility == Visibility.Visible && lockEditorView.Visibility != Visibility.Collapsed)
            {
                lockEditorView.Visibility = Visibility.Collapsed;
                gameDataEditorView.Visibility = Visibility.Visible;
            }

            if (nodeWrapper is LoadNodeWrapper)
            {
                loadNodeEditorView.Visibility = System.Windows.Visibility.Visible;
                loadNodeEditorView.SetLoadNode(nodeWrapper as LoadNodeWrapper);
            }
            else
            {
                loadNodeEditorView.Visibility = System.Windows.Visibility.Collapsed;
                loadNodeEditorView.SetLoadNode(null);
            }
        }

        private void LoadLinkEditor(LinkWrapper linkWrapper)
        {
            lockEditorView.SetLink(linkWrapper);

            if (propertiesPanel.Visibility == Visibility.Visible && gameDataEditorView.Visibility != Visibility.Collapsed)
            {
                lockEditorView.Visibility = Visibility.Visible;
                gameDataEditorView.Visibility = Visibility.Collapsed;
            }

            loadNodeEditorView.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void ViewModel_SelectedItemChanged(object selectedItem)
        {
            if (selectedItem is LinkWrapper)
                LoadLinkEditor(selectedItem as LinkWrapper);
            else if (selectedItem is NodeBaseWrapper)
                LoadGameDataEditor(selectedItem as NodeBaseWrapper);
            else
            {
                gameDataEditorView.SetGameData(null);
                lockEditorView.SetLink(null);
            }
        }
    }
}
